﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class DocPostMarkController : Base
{
    private readonly ApplicationContext _applicationContext;
    private readonly DocPostMarkService _docPostMarkService;

    public DocPostMarkController(
        ApplicationContext applicationContext,
        DocPostMarkService docPostMarkService
        )
    {
        _applicationContext = applicationContext;
        _docPostMarkService = docPostMarkService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.DocPost_Mark)]
    public async Task<IActionResult> GetDocPostMark(int id)
    {
        ViewDocPostMark articleMark = await _docPostMarkService.GetOneAsync(id);
        return Success(new { data = articleMark });
    }

    [HttpPost]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.DocPost_Mark)]
    [Log(Name = "标记报送文件")]
    public async Task<IActionResult> InsertOrUpdateAsync([FromBody] EditDocPostMark mark)
    {
        ILoginUserInfo adminUser = await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync();
        if (adminUser.PostCode != UserPostConst.MASTER && adminUser.PostCode != UserPostConst.DEPUTY)
            return Fail($"必须职位 ({UserPostConst.MASTER}) 或 ({UserPostConst.DEPUTY}) 才能标记");
        mark.Uid = adminUser.UserId;
        DocPostMark postMark = mark.Adapt<DocPostMark>();  
        bool res = await _docPostMarkService.InsertOrUpdateAsync(postMark);
        if (res)
            return Success("标记文件 ID：" + mark.Aid);
        else
            return Fail("标记文件 ID：" + mark.Aid);
    }

    [HttpDelete]
    [Log(Name = "删除信息报送标记")]
    [ActionPermissionFilter(Permission = PermissionKeys.DocPost_Mark)]
    public async Task<IActionResult> Delete(int id)
    {
        ILoginUserInfo adminUser = await _applicationContext.ILoginUserInfoMethods.LoginUserInfoAsync();
        if (adminUser.PostCode != UserPostConst.MASTER && adminUser.PostCode != UserPostConst.DEPUTY)
            return Fail($"必须职位 ({UserPostConst.MASTER}) 或 ({UserPostConst.DEPUTY}) 才能删除标记");
        if (await _docPostMarkService.DeleteAsync(id) > 0)
            return Success("删除信息报送的标记 ID：" + id);
        else
            return Fail("删除失败！");
    }
}


